const app = getApp()
const { formatObjToUrl, RandomColor } = require('../../utils/util')
// pages/suits/suits.js
Page({

  data: {
    suitOptions: [],
    suitType: '',
    level: -1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取试题套数
    this.getSuitOptions({ subject_type: options.suitType, level: options.level })
    this.setData({
      suitType: options.suitType,
      level: options.level
    })
  },

  // 获取试题套数
  getSuitOptions(submitData) {
    app.axios({
      url: `/study/subjects/items${formatObjToUrl(submitData)}`
    }).then(res => {
      if(res.code === 0) {
        this.setData({
          suitOptions: res.result.map(item => {
            return {
              suitId: item.itemId,
              suitName: item.item,
              bgColor: new RandomColor().color
            }
          })
        })
      }
    })
  },

  // 跳转页面开始答题
  goToQuestions(e) {
    const options = {
      suitType: this.data.suitType,
      suitId: e.currentTarget.dataset.suitid,
      level: this.data.level
    }
    wx.navigateTo({
      url: `../questions/questions${formatObjToUrl(options)}`,
    })
  }
})